#pragma once
#include "..\..\include\tstl.h"
#include <list>
#include <locale>
#include <algorithm>
#include <functional>


typedef std::list<std::tstring> LST_NAMES;


class CNameListFile
	: protected std::tifstream
	, public LST_NAMES
{
public:

public:
	CNameListFile( std::tstring strFileName )
		: std::tifstream( strFileName.c_str() )
		, LST_NAMES()
	{
		if( !fail() )
		{
			Read();
		}
	}

	~CNameListFile(void)
	{
	}

private:
	void Read( void );
	void ReadLine( std::tstring& strLine );
	void AddName( std::tstring strLine );
	static bool IsValidChar( TCHAR c );
};

inline void CNameListFile::Read( void )
{
	while( true )
	{
		std::tstring strLine;
		ReadLine( strLine );
		AddName( strLine );
		if( eof() )
		{
			break;
		}
	}
}

inline void CNameListFile::ReadLine( std::tstring& strLine )
{
	this->imbue( g_locale );
	std::getline( *this, strLine );
}

inline void CNameListFile::AddName( std::tstring strLine )
{
	std::tstring::iterator iterWhitespace = std::remove_if( strLine.begin(), strLine.end(), IsValidChar );
	strLine.erase( iterWhitespace, strLine.end() );

	if( strLine.empty() )
	{
		return;
	}

	LST_NAMES::iterator iterFound = std::find( LST_NAMES::begin(), LST_NAMES::end(), strLine );
	if( iterFound == LST_NAMES::end() )
	{
		LST_NAMES::push_back( strLine );
	}
}

inline bool CNameListFile::IsValidChar( TCHAR c )
{
	return std::isspace( c, g_locale );
}
